package com.hptpd.questionnairesurvey.service.excel;

import com.hptpd.questionnairesurvey.assets.Constant;
import com.hptpd.questionnairesurvey.domain.Choice;
import com.hptpd.questionnairesurvey.domain.Questionnaire;
import com.hptpd.questionnairesurvey.domain.question.FillsUpQuestion;
import com.hptpd.questionnairesurvey.domain.question.SingleChoiceQuestion;
import com.hptpd.questionnairesurvey.repository.*;
import com.hptpd.questionnairesurvey.vo.excel.ImportQuestionnaireVo;
import lombok.Data;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @author YMHUI
 * @description 导入试卷
 */
@Data
public class ImportQuestionnaire {
    private MultipartFile excelFile;

    private XSSFWorkbook workbook;

    private final QuestionnaireRep questionnaireRep;

    private ImportQuestionnaireVo importQuestionnaireVo;

    private BaseQuestionRep questionRep;

    private ChoiceRep choiceRep;

    private Questionnaire questionnaire;

    public ImportQuestionnaire(MultipartFile excelFile, QuestionnaireRep questionnaireRep,
                               BaseQuestionRep questionRep, ChoiceRep choiceRep) {
        this.excelFile = excelFile;
        this.questionnaireRep = questionnaireRep;
        this.questionRep = questionRep;
        this.choiceRep = choiceRep;
    }

    public boolean initWorkBook() {
        try {
            if (excelFile == null) {
                return false;
            }
            workbook = new XSSFWorkbook(excelFile.getInputStream());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean parseQuestions() {
        if (!initWorkBook()) {
            return false;
        }
        buildQuestionnaire();
        XSSFSheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        for (int num = 1; num <= rowNum; num ++) {
            XSSFRow row = sheet.getRow(num);
            XSSFCell cell = row.getCell(5);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
                int type = (int) cell.getNumericCellValue();
                switch (type) {
                    case 0:
                        addSingle(questionnaire, row);
                        break;
                    case 1:
                        addFillUp(questionnaire, row);
                        break;
                    default:
                        break;
                }
            }
        }
        return true;
    }

    private void buildQuestionnaire() {
        questionnaire = new Questionnaire();
        questionnaire.setName(FilenameUtils.getBaseName(excelFile.getOriginalFilename()));
        questionnaire.setState(Constant.QUESTIONNAIRE_NOTISSUE);
        questionnaire.setCreatedAt(new Date());
        questionnaire = questionnaireRep.save(questionnaire);
    }

    private void addFillUp(Questionnaire questionnaire, XSSFRow row) {
        FillsUpQuestion fillsUpQuestion = new FillsUpQuestion();
        fillsUpQuestion.setTopic(row.getCell(6).getStringCellValue());
        fillsUpQuestion.setSort((int) row.getCell(0).getNumericCellValue());
        fillsUpQuestion.setQuestionnaire(questionnaire);
        questionRep.save(fillsUpQuestion);
    }

    private void addSingle(Questionnaire questionnaire, XSSFRow row) {
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTopic(row.getCell(6).getStringCellValue());
        singleChoiceQuestion.setSort((int) row.getCell(0).getNumericCellValue());
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion = questionRep.save(singleChoiceQuestion);
        int cellNum = row.getLastCellNum();
        for (int num = 7; num <= cellNum; num ++) {
            if (row.getCell(num) == null) {
                continue;
            }
            String cellValue = row.getCell(num).getStringCellValue();
            if (StringUtils.isNotBlank(cellValue)) {
                Choice choice = new Choice();
                choice.setBaseQuestion(singleChoiceQuestion);
                choice.setDescription(cellValue);
                choice.setScore(5);
                choiceRep.save(choice);
            }
        }
    }
}
